<?php

declare (strict_types=1);

namespace App\Model;

use App\Service\Cache\CooperativeMenuConfigCache;
use Hyperf\DbConnection\Model\Model;

/**
 * @property \Hyperf\Utils\HigherOrderTapProxy|mixed|void|null menu_config_id
 * @property \Hyperf\Utils\HigherOrderTapProxy|mixed|void|null title
 * @property \Hyperf\Utils\HigherOrderTapProxy|mixed|void|null name
 * @property \Hyperf\Utils\HigherOrderTapProxy|mixed|void|null type
 * @property \Hyperf\Utils\HigherOrderTapProxy|mixed|void|null api
 * @property \Hyperf\Utils\HigherOrderTapProxy|mixed|void|null path
 * @property \Hyperf\Utils\HigherOrderTapProxy|mixed|void|null pid
 * @property \Hyperf\Utils\HigherOrderTapProxy|mixed|void|null id
 */
class CooperativeMenu extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'cooperative_menu';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['menu_config_id', 'cooperative_id'];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [];

    protected $appends = ['type', 'api', 'path', 'name', 'title', 'pid'];

    protected $menuConfigCache = [];

    public function getCooperativeMenuConfigAttribute()
    {
        return $this->menuConfigCache();
    }

    public function getPidAttribute()
    {
        return $this->menuConfigCache()['pid'];
    }

    public function getNameAttribute()
    {
        return $this->menuConfigCache()['name'];
    }

    public function getTitleAttribute()
    {
        return $this->menuConfigCache()['title'];
    }

    public function getTypeAttribute()
    {
        return $this->menuConfigCache()['type'];
    }

    public function getPathAttribute()
    {
        return isset($this->menuConfigCache()['path']) ? $this->menuConfigCache()['path'] : '';
    }

    public function getApiAttribute()
    {
        return isset($this->menuConfigCache()['api']) ? $this->menuConfigCache()['api'] : '';
    }

    public function menuConfigCache()
    {
        if (empty($this->menuConfigCache)) {
            $this->menuConfigCache = di(CooperativeMenuConfigCache::class)->get($this->menu_config_id);
        }
        return $this->menuConfigCache;
    }

    public function isApi()
    {
        return $this->menuConfigCache()['type'] == \App\Constants\Model\CooperativeMenuConfig::TYPE_API;
    }

    public function isPath()
    {
        return $this->menuConfigCache()['type'] == \App\Constants\Model\CooperativeMenuConfig::TYPE_PATH;
    }
}